package loadiconhandler

import (
	"fmt"
	"gmap-backend/internal/config"
	"gmap-backend/internal/server/vo"
	"gmap-backend/internal/util/pathutil"
	"strings"
)

type CmdOpenPathLoadIcon struct {
}

func (owner *CmdOpenPathLoadIcon) CanHandle(appConfig *config.AppConfig, req *vo.PathReq) (bool, any) {
	urlMatch := strings.HasPrefix(req.Path, "cmdopen:///") || strings.HasPrefix(req.Path, "cmdopen://")
	if urlMatch {
		path := strings.TrimSpace(strings.TrimPrefix(strings.TrimPrefix(req.Path, "cmdopen:///"), "cmdopen://"))
		return true, path
	}
	return false, nil
}

func (owner *CmdOpenPathLoadIcon) Handle(appConfig *config.AppConfig, req *vo.PathReq, extra any) *vo.LoadIconResp {
	path := extra.(string)
	parseResult := pathutil.ParseValidPath(path, req.CtxDir)
	if !parseResult.Succ {
		panic(fmt.Sprintf("无法匹配到有效路径：\r\n%s", path))
	}
	if parseResult.File {
		panic(fmt.Sprintf("不支持对文件执行此操作：\r\n%s", path))
	}
	return &vo.LoadIconResp{
		Cascade: true,
		Url:     SPECIAL_PATH_CMD,
		Url2:    SPECIAL_PATH_FOLDER,
	}
}
